package com.xsky.logic.knowledge;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

import com.xsky.common.util.UUIDUtil;
import com.xsky.common.util.XMLConverter;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.operation.vo.TabKnowledgeBase;
import com.xsky.operation.vo.TabKnowledgeModifyHistory;
import com.xsky.operation.vo.TabPlatFault;

public class KnowledgeLogic {

	BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();

	KnowledgeHistoryLogic history = new KnowledgeHistoryLogic();

	// 增加
	public String add(String xmlStr) {
		TabKnowledgeBase entity = XMLConverter.convertToObject(xmlStr, TabKnowledgeBase.class);
		if (StringUtils.isEmpty(entity.getId())) {
			entity.setId(UUIDUtil.getUUID());
		}
		entity.setTimestamp(new Date());
		baseDao.insertEntity(entity);
		history.add(entity, "ADD");
		return "<data><result>ok</result><uuid>" + entity.getId() + "</uuid><info>添加成功</info></data>";
	}

	// 删除
	public String delete(String id) {
		TabKnowledgeBase entity = queryById0(id);
		baseDao.delete(entity);
		history.add(entity, "DELETE");
		return "<data><result>ok</result><uuid></uuid><info>删除成功</info></data>";
	}

	public String queryById(String id) {
		TabKnowledgeBase entity = queryById0(id);
		if(entity ==null){
			return null;
		}
		return XMLConverter.convertToXML(entity);
	}

	private TabKnowledgeBase queryById0(String id) {
		List list = baseDao.findByKey(new TabKnowledgeBase(), "id", id);

		if (list == null || list.isEmpty()) {
			return null;
		}
		TabKnowledgeBase entity = (TabKnowledgeBase) list.get(0);
		return entity;
	}

	// 更新报送信息
	public String update(String xmlStr) {
		TabKnowledgeBase entity = XMLConverter.convertToObject(xmlStr, TabKnowledgeBase.class);
		entity.setTimestamp(new Date());
		baseDao.updateEntity(entity);
		history.add(entity, "UPDATE");
		return "<data><result>ok</result><uuid></uuid><info>更新成功</info></data>";
	}

	public String genAll() {
		List list = baseDao.findAll(TabKnowledgeBase.class.getName());
		return XMLConverter.convertToXML(list);
	}

	public String queryByCondition(String xml) {
		// xml 举例 :
		// <data><caseTypeName>asdfasdl</caseTypeName><keyword>解决</keyword><pageNum>4</pageNum><numPerPage>4</numPerPage></data>

		try {
			Document document = DocumentHelper.parseText(xml);
			String caseTypeName = document.selectSingleNode("//data//caseTypeName").getText().trim();
			String keyword = document.selectSingleNode("//data//keyword").getText().trim();
			int pageNum = Integer.parseInt(document.selectSingleNode("//data//pageNum").getText().trim());
			int numPerPage = Integer.parseInt(document.selectSingleNode("//data//numPerPage").getText().trim());

			if (pageNum < 2) {
				pageNum = 1;
			}

			String start = (1 + ((pageNum - 1) * numPerPage)) + "";

			StringBuilder sb = new StringBuilder(" 1=1 ");
			if(StringUtils.isNotEmpty(caseTypeName)){
				sb.append(" and caseTypeName ='" + caseTypeName + "'");
			}
			if(StringUtils.isNotEmpty(keyword)){
				sb.append(" and caseDesc like '%" + keyword + "%'");
			}
			
			// 这接口用起来快死人了....
			List list = baseDao.fetchByPagesWithCondition(TabKnowledgeBase.class.getName(), start, numPerPage + "",
					sb.toString(), "createTime", "asc");
			return XMLConverter.convertToXML(list);

		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String queryHistory(String id) {
		List list = baseDao.findByKey(new TabKnowledgeModifyHistory(), "baseId", id);
		return XMLConverter.convertToXML(list);
	}
}
